package com.smartEdu.commons;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class CMap {
	private HashMap<String, Object> sess = new HashMap<String, Object>();
	
	public void SetAll(Map<String, ?> map){
		sess.putAll(map);
	}
	public Set<String> getKeys() {
		return sess.keySet();
	}

	public HashMap<String, Object> getVals() {
		return sess;
	}

	public void put(String key, Object val) {
		sess.put(key.trim(), val);
	}
	
	public void put(String key,Object val,String defval){
		if(val == null || val.toString().trim().equalsIgnoreCase("null"))sess.put(key, defval);
		else		   sess.put(key, val);
	}

	public Object geto(String key) {
		return sess.get(key.trim()) == null ? null : sess.get(key.trim());
	}
	
	public HashMap<?, ?> getmap(String key){
		return (HashMap<?, ?>)geto(key);
	}

	public String gets(String key) {
		return (String) geto(key);
	}
	
	public String gets_allownull(String key){
		return geto(key) == null ? "" : (String)geto(key);
	}

	public int geti(String key) {
		return v2i(geto(key));
	}

	public double getd(String key) {
		return v2d(geto(key));
	}

	public void remove(String key) {
		sess.remove(key.trim());
	}

	public void clear() {
		sess.clear();
	}

	private int v2i(Object v) {
		if (v instanceof String)
			return Integer.parseInt((String) v);
		if (v instanceof Integer)
			return ((Integer) v).intValue();
		return 0;
	}

	private double v2d(Object v) {
		if (v instanceof String)
			return Double.parseDouble((String) v);
		if (v instanceof Double)
			return ((Double) v).doubleValue();
		return 0d;
	}
}
